package Future;

import java.text.SimpleDateFormat;
import java.util.Date;

public class GetTimeLag {
	public static void main(String[] args) throws InterruptedException {
		// Date begin = new Date();
		// Thread.sleep(3000);
		// Date end = new Date();
		// getTimeLag(begin, end);
		getTimeGap();
	}

	public static String getCurrentTime() {
		// 2012-9-10 11:50:18
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String currentTime = bartDateFormat.format(date);
		return currentTime;
	}

	public static String getTimeLag(Date begin, Date end) {
		long between = 0;
		try {
			between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		long day = between / (24 * 60 * 60 * 1000);
		long hour = (between / (60 * 60 * 1000) - day * 24);
		long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
				- min * 60 * 1000 - s * 1000);
		System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒" + ms
				+ "毫秒");
		return day + "天" + hour + "小时" + min + "分" + s + "秒" + ms + "毫秒";
	}

	public static String getTimeGap() {
		SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		long between = 0;
		try {
			java.util.Date begin = dfs.parse("2009-07-10 10:22:21.214");
			java.util.Date end = dfs.parse("2009-07-20 11:24:49.145");
			between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		long day = between / (24 * 60 * 60 * 1000);
		long hour = (between / (60 * 60 * 1000) - day * 24);
		long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
				- min * 60 * 1000 - s * 1000);
		System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒" + ms
				+ "毫秒");
		return day + "天" + hour + "小时" + min + "分" + s + "秒" + ms + "毫秒";
	}
}
